/**
 * 
 */
package com.youxin.cpcenter.service.impl;

import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youxin.busfoundation.bean.leads.LeadsStatTotalBean;
import com.youxin.busfoundation.bean.leads.QueryLeadsViewBean;
import com.youxin.busfoundation.bean.login.UserInfo;
import com.youxin.busfoundation.common.es.bean.CompanyInfoBean;
import com.youxin.busfoundation.model.EntCluePoolBean;
import com.youxin.cpcenter.dao.TbEntLeadsViewMapper;
import com.youxin.cpcenter.model.TbEntLeadsView;
import com.youxin.cpcenter.service.ViewLeadsService;
import com.youxin.foundation.db.utils.KeyGenerationUtils;
import com.youxin.foundation.utils.ArrayUtils;

/**
 * 是否查看线索服务
 * 
 * @description
 * @author yingde.cao
 * @date 2017年12月15日
 * @version 1.0
 */
@Service("viewLeadsService")
public class ViewLeadsServiceImpl implements ViewLeadsService {

	@Autowired
	private TbEntLeadsViewMapper tbEntLeadsViewMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.youxin.cpcenter.service.ViewLeadsService#isViewLeadsId(long,
	 * long)
	 */
	@Override
	public boolean isViewLeadsId(long leadsId, long entMemberId) {
		QueryLeadsViewBean bean = new QueryLeadsViewBean();
		bean.setCluePoolId(leadsId);
		bean.setEntMemberId(entMemberId);
		LeadsStatTotalBean result = tbEntLeadsViewMapper.selectByEntMemberIdLeadsId(bean);
		return result.getTotal() > 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.youxin.cpcenter.service.ViewLeadsService#updateViewLeads(long,
	 * com.youxin.busfoundation.bean.login.UserInfo)
	 */
	@Override
	public void updateViewLeads(String companyUid, UserInfo userInfo) {
		TbEntLeadsView entity = new TbEntLeadsView();
		entity.setId(KeyGenerationUtils.generateKey());
		entity.setEntMemberId(userInfo.getEntMemberId());
		entity.setCompanyUid(companyUid);
		entity.setMemberId(userInfo.getId());
		tbEntLeadsViewMapper.insertSelectiveIgnore(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.youxin.cpcenter.service.ViewLeadsService#changeLeadsViewProperty(java
	 * .util.List, com.youxin.busfoundation.bean.login.UserInfo)
	 */
	@Override
	public void updateLeadsPoolProperty(List<EntCluePoolBean> list, long entMemberId) {
		List<String> ids = list.stream().map((t) -> {
			return t.getCompanyUid();
		}).collect(Collectors.toList());
		if(ArrayUtils.isEmpty(ids)){
			return;
		}
		QueryLeadsViewBean bean = new QueryLeadsViewBean();
		bean.setIds(ids);
		bean.setEntMemberId(entMemberId);
		List<String> results = tbEntLeadsViewMapper.selectByEntMemberIdLeads(bean);
		if (!ArrayUtils.isEmpty(results)) {
			for (EntCluePoolBean item : list) {
				if (results.contains(item.getCompanyUid())) {
					item.setViewStatus(1);
				}
			}
		}
	}

	public void updateLeadsViewStatus(List<CompanyInfoBean> list, long entMemberId) {
		List<String> ids = list.stream().map((t) -> {
			return t.getUid();
		}).collect(Collectors.toList());
		if(ArrayUtils.isEmpty(ids)){
			return;
		}

		QueryLeadsViewBean bean = new QueryLeadsViewBean();		
		bean.setIds(ids);
		
		bean.setEntMemberId(entMemberId);
		
		List<String> results = tbEntLeadsViewMapper.selectByEntMemberIdLeads(bean);
		if (!ArrayUtils.isEmpty(results)) {
			for (CompanyInfoBean item : list) {
				if (results.contains(item.getUid())) {
					item.setViewStatus(1);
				}
			}
		}
	}

}
